package com.uuah.server.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.exception.lifecycle.InitialisationException;
import com.uuah.server.UuahServer;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.config.IConfigurationBuilder;
import com.uuah.server.api.context.IUuahContextBuilder;
import com.uuah.server.api.context.IUuahContextFactory;
import com.uuah.server.exception.config.ConfigurationException;

/**
 * Default implementation that stores UuahContext in {@link UuahServer} static
 * and uses {@link UuahContextBuilder} to build new {@link IUuahContext} instances
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:35:14
 * @version 1.0.0
 */
public class UuahContextFactory implements IUuahContextFactory {
	protected transient final Logger logger = LoggerFactory
			.getLogger(UuahContextBuilder.class);

	public IUuahContext createUuahContext(
			IConfigurationBuilder configurationBuilder,
			IUuahContextBuilder uuahContextBuilder)
			throws InitialisationException, ConfigurationException {

		IUuahContext uuahContext = doCreateUuahContext(uuahContextBuilder);

		configurationBuilder.configure(uuahContext);

		return uuahContext;
	}

	public IUuahContext createUuahContext(
			IConfigurationBuilder configurationBuilder)
			throws InitialisationException, ConfigurationException {

		IUuahContext uuahContext = doCreateUuahContext(new UuahContextBuilder());

		configurationBuilder.configure(uuahContext);

		return uuahContext;
	}

	protected IUuahContext doCreateUuahContext(
			IUuahContextBuilder uuahContextBuilder)
			throws InitialisationException {
		IUuahContext uuahContext = buildUuahContext(uuahContextBuilder);
		UuahServer.setUuahContext(uuahContext);

		uuahContext.initialise();

		return uuahContext;
	}

	protected IUuahContext buildUuahContext(IUuahContextBuilder uuahContextBuilder) {
		return uuahContextBuilder.buildUuahContext();
	}

}
